﻿using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

	public float gravity = 50.0f;
	public float jumpForce = 600.0f;
	//是否无敌（用于测试）
	public bool isInvincible = true;
	public LayerMask groundMask;
	public Transform groundPos;
	//死亡效果
	public Transform deathParticles;

	private bool isGrounded = false;
	private bool isDead = false;
	private float groundCheckRadius = 0.2f;
	private Collider2D[] tmpCollider = new Collider2D[1];
	private RaycastHit2D[] tmpHit = new RaycastHit2D[1];
	private Vector3 startPos;
	private Animator anim;

	private float playerSize = 0.5f;

	void Start()
	{
		isGrounded = false;
		startPos = transform.position;
		anim = GetComponent<Animator> ();

		gameObject.SetActive (false);
	}
	/// <summary>
	/// 重新开始
	/// </summary>
	public void Restart()
	{
		transform.position = startPos;
		isGrounded = false;
		isDead = false;
		gameObject.SetActive (true);


	}
	/// <summary>
	/// 判断玩家是否出屏幕
	/// </summary>
	/// <returns><c>true</c> if this instance is player off screen; otherwise, <c>false</c>.</returns>
	bool IsPlayerOffScreen()
	{
		float screenBottomEdge = -Camera.main.orthographicSize;
		float screenLeftEdge = -Camera.main.aspect * Camera.main.orthographicSize;

		return (transform.position.x + playerSize < screenLeftEdge ||
				transform.position.y + playerSize < screenBottomEdge);
	}
	/// <summary>
	/// Raises the trigger enter2 d event.
	/// </summary>
	/// <param name="other">Other.</param>
	void OnTriggerEnter2D(Collider2D other)
	{
		if (isDead)
			return;

		if (other.CompareTag ("Spikes") && !isInvincible)
			KillPlayer ();
	}
	/// <summary>
	/// Kills the player.
	/// </summary>
	void KillPlayer()
	{
		Transform particles = (Transform)Instantiate (deathParticles, transform.position, transform.rotation);
		Destroy (particles.gameObject, 2.0f);

		OnPlayerDeath ();
	}
	/// <summary>
	/// Handles the tap.
	/// </summary>
	public void HandleTap()
	{
		// handle touch/mouse input
		DoJump ();
	}
	/// <summary>
	/// Raises the player death event.
	/// </summary>
	void OnPlayerDeath()
	{
		if (!isDead)
		{
			isDead = true;
			gameObject.SetActive (false);
			SoundManager.PlaySfx ("Death");
			GameManager.Game.OnPlayerDeath ();
		}
	}
	/// <summary>
	/// Update this instance.
	/// </summary>
	void Update()
	{
		if (GameManager.Game.IsPaused || isDead)
			return;

		if (Input.GetButtonDown ("Jump") ||Input.GetMouseButtonDown(0))
			DoJump ();

		if (IsPlayerOffScreen ())
			OnPlayerDeath ();
	}
	/// <summary>
	/// 人物跳跃
	/// </summary>
	void DoJump()
	{
		if (isGrounded)
		{
			Vector2 vel = GetComponent<Rigidbody2D> ().velocity;
			vel.y = 0.0f;
			GetComponent<Rigidbody2D> ().velocity = vel;
			GetComponent<Rigidbody2D> ().AddForce (new Vector2(0, jumpForce));
		}
	}
	/// <summary>
	/// Fixeds the update.
	/// </summary>
	void FixedUpdate()
	{
		if (GameManager.Game.IsPaused || isDead)
			return;

		isGrounded = Physics2D.OverlapCircleNonAlloc (groundPos.position, groundCheckRadius, tmpCollider, groundMask) > 0;
		bool hitWall = Physics2D.RaycastNonAlloc (transform.position + 0.3f*Vector3.up, Vector3.right, tmpHit, 0.1f, groundMask) > 0;

		if (hitWall && !isInvincible)
		{
			KillPlayer ();
			return;
		}

		GetComponent<Rigidbody2D> ().AddForce (-gravity * Vector2.up);

		Vector3 pos = transform.position;
		pos.x = startPos.x;
		transform.position = pos;

		anim.SetFloat ("ySpeed", GetComponent<Rigidbody2D>().velocity.y);
		anim.SetBool ("IsGrounded", isGrounded);
	}
}
